<template>
    <div class="shopCart">
        <div class="cartList">
            <ul v-if="dataList.length > 0">
                <li v-for="item in dataList" :key="item.id">
                    <van-checkbox
                        :value="item.id"
                        v-model="item.isChecked"
                        checked-color="#15C481"
                        @click="chooseChange(item.id, item)"
                    ></van-checkbox>
                    <div class="shopdetail">
                        <div class="detailtext">
                            <div class="shoptitle van-multi-ellipsis--l2">
                                {{ item.name }}
                            </div>
                            <div class="shoppricenum">
                                <p class="shopprice">
                                    可申领数量 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                                </p>
                                <p class="shopprice">
                                    {{
                                        item.inStockNumber
                                    }}
                                </p>
                                <div class="shopnum">
                                    <van-stepper v-model="item.number" input-width="50px" integer min="1"
                                                 :max="item.inStockNumber"
                                                 @change="onChange(item)"/>
                                </div>
                            </div>
                        </div>
                    </div>
                </li>
            </ul>
            <div class="nohaveshop" v-else>
                <van-icon name="shopping-cart-o"/>
                <!--        <p class="p1">你的购物车空空如也~~</p>-->
                <!--        <p class="p2">快去采购吧!</p>-->
            </div>
        </div>
        <div class="cartfotter" v-if="dataList.length > 0">
            <van-submit-bar button-text="提交" @submit="onSubmit">
                <van-checkbox
                    v-model="allchecked"
                    checked-color="#15C481"
                    @click="checkAll"
                >全选
                </van-checkbox
                >
                <div class="buyprice">
                </div>
            </van-submit-bar>
        </div>
    </div>
</template>

<script>
import {Icon, Checkbox, Stepper, SubmitBar, Toast} from "vant";
import {Config} from "@/config/formDataOptions";
import {formatDate} from "@/components/date";
import {API_URL} from "@/config/urlInfo";

export default {
    components: {
        [Icon.name]: Icon,
        [Checkbox.name]: Checkbox,
        [Stepper.name]: Stepper,
        [SubmitBar.name]: SubmitBar,
        [Toast.name]: Toast
    },
    data() {
        return {
            dataList: [],
            allchecked: false,
            selectedData: [],
            userModel: {
                shenqingTrueName: "",
                shenqingDepName: "",
                applyDate: formatDate(new Date(), "yyyy-MM-dd HH:mm:ss"),
                shenqingDepId: '',
                uids: ''
            },
        };
    },
    computed: {},
    mounted() {
        this.getData();
        this.getUidsAndTureName()
    },
    methods: {
        getData() {
            // Config.getDatas("/office/api/article/mobileList", {"type": 1}, (res) => {
            Config.getDatas(API_URL.mobileList, {"type": 1}, (res) => {
                this.dataList = res.list
            }, this)
        },

        // 单选的change事件
        chooseChange(i, item) {
            if (this.selectedData.indexOf(i) > -1) {
                console.log(i);
                var arrs = this.selectedData.filter(function (item) {
                    return item != i;
                });
                this.selectedData = arrs;
                item.isChecked = false;
            } else {
                this.selectedData.push(i);
                item.isChecked = true;
            }
            if (this.selectedData.length < this.dataList.length) {
                this.allchecked = false;
            } else {
                this.allchecked = true;
            }
        },
        // 商品数量
        // eslint-disable-next-line no-unused-vars
        onChange(item) {

        },
        // 全选
        checkAll() {
            let list = this.dataList;
            if (this.allchecked === false) {
                list.forEach(element => {
                    element.isChecked = false;
                });
                this.selectedData = [];
            } else {
                list.forEach(element => {
                    element.isChecked = true;
                    if (this.selectedData.indexOf(element.id) < 0) {
                        this.selectedData.push(element.id);
                    }
                });
            }
        },
        getUidsAndTureName() {
            // Config.getDatas("/office/api/repair/getMobileUidAndName", {}, res => {
            Config.getDatas(API_URL.getMobileUidAndName, {}, res => {
                this.userModel.shenqingTrueName = res.ture_name
                this.userModel.uids = res.uids
                this.userModel.shenqingDepName = res.depArr[0].name
                this.userModel.shenqingDepId = res.depArr[0].id
            }, this)
        },
        // 去结算
        onSubmit() {
            // 选择购买的商品
            var cartgoods = [];
            this.dataList.forEach(function (item) {
                if (item.isChecked) {
                    cartgoods.push({
                        formModel: {
                            wupinId: item.id,
                            number: item.number, desc: "",
                            inStockNumber: item.inStockNumber,
                            receiveDate: new Date(),
                            wupinName: item.name
                        }
                    });
                }
            });
            if (cartgoods.length === 0) {
                Toast("请选择申领物品");
            } else {
                this.userModel.dataList = cartgoods
                // eslint-disable-next-line no-unused-vars
                // Config.postDatas("/office/api/claim/claimMobileSaveList", {data: this.userModel}, res => {
                Config.postDatas(API_URL.claimMobileSaveList, {data: this.userModel}, res => {
                    Toast("申请成功");
                    this.$router.go(-1)
                }, this)
            }
        }
    }
};
</script>

<style lang="scss" scoped>
.shopCart {
    width: 100%;
    min-height: 100vh;
    display: flex;
    flex-direction: column;
    background-color: #f6f6f6;

    .cartList {
        width: 100%;
        display: flex;
        flex-direction: column;
        align-items: center;
        margin-top: 16px;

        ul {
            width: 100%;
            display: flex;
            flex-direction: column;
            align-items: center;
            margin-bottom: 100px;

            li {
                width: 100%;
                height: 96px;
                background-color: #fff;
                display: flex;
                flex-direction: row;
                align-items: center;
                margin-bottom: 12px;

                .van-checkbox {
                    margin-left: 17px;

                    ::v-deep .van-checkbox__icon {
                        height: 14px;
                        line-height: 14px;

                        .van-icon {
                            width: 14px;
                            height: 14px;
                            font-size: 12px;
                            border: 1px solid #a5a5a5;
                        }
                    }
                }

                .shopdetail {
                    display: flex;
                    flex-direction: row;
                    align-items: center;
                    margin-left: 13px;

                    .detailimg {
                        width: 64px;
                        height: 64px;
                        background: rgba(165, 165, 165, 1);
                        border-radius: 4px;

                        img {
                            width: 100%;
                            height: 100%;
                            border-radius: 4px;
                        }
                    }

                    .detailtext {
                        width: 300px;
                        height: 60px;
                        display: flex;
                        flex-direction: column;
                        margin-left: 8px;
                        position: relative;

                        .shoptitle {
                            width: 180px;
                            text-align: justify;
                            font-size: 12px;
                            color: #212121;
                            line-height: 17px;
                        }

                        .shoppricenum {
                            width: 100%;
                            display: flex;
                            flex-direction: row;
                            align-items: center;
                            justify-content: space-between;
                            position: absolute;
                            bottom: 0px;

                            .shopprice {
                                font-size: 12px;
                                color: #15c481;
                                font-weight: 600;
                            }

                            .shopnum {
                                display: flex;

                                ::v-deep .van-stepper {
                                    button {
                                        width: 14px;
                                        height: 14px;
                                        border: 1px solid #333333;
                                        border-radius: 50px;
                                        background-color: #fff;
                                    }

                                    .van-stepper__minus::before {
                                        width: 8px;
                                    }

                                    .van-stepper__plus::before {
                                        width: 8px;
                                    }

                                    .van-stepper__plus::after {
                                        height: 8px;
                                    }

                                    .van-stepper__input {
                                        font-size: 12px;
                                        color: #333333;
                                        background-color: #fff;
                                        padding: 0px 2px;

                                    }
                                }
                            }
                        }
                    }
                }
            }
        }

        .nohaveshop {
            display: flex;
            flex-direction: column;
            align-items: center;
            margin-top: 100px;

            .van-icon {
                font-size: 60px;
                color: #666;
            }

            p {
                font-size: 14px;
                color: #999;
            }

            .p1 {
                margin-top: 20px;
            }
        }
    }

    .cartfotter {
        width: 100%;
        height: 60px;
        position: fixed;
        bottom: 0;
        left: 0;

        .van-submit-bar__bar {
            height: 60px;
            font-size: 16px;

            .van-checkbox {
                margin-left: 17px;

                ::v-deep .van-checkbox__icon {
                    height: 14px;
                    line-height: 14px;

                    .van-icon {
                        width: 14px;
                        height: 14px;
                        font-size: 12px;
                        border: 1px solid #a5a5a5;
                    }
                }

                ::v-deep .van-checkbox__label {
                    font-size: 16px;
                    color: #212121;
                    margin-left: 9px;
                }
            }

            .buyprice {
                flex: 1;
                padding-right: 8px;
                text-align: right;
                display: flex;
                flex-direction: column;

                .p1 {
                    font-size: 10px;
                    color: #001410;
                }

                .p2 {
                    font-size: 12px;
                    color: #15c481;
                    margin-top: 4px;
                }
            }

            .van-button--danger {
                width: 130px;
                height: 60px;
                background: rgba(21, 196, 129, 1);
                border: none;
                font-size: 16px;
                color: #ffffff;
            }
        }
    }
}
</style>